<?php
/**
 * 订单操作
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */


class OrderService extends ServiceBase {

    private $order = null;

    public function __construct($order){
        $this->order = $order;
        return parent::__construct();
    }

    /**
     * 会员积分账户比例入账
     *
     * @param $money 赠送款额
     * @param $accountType 赠送类型
     * @param string $description
     * @return $this|bool
     */
    public function income($money, $accountType = MemberAccount::INTEGRAL_ACCOUNT, $description = ''){
        $memberAccount = $this->order->member->getAccount($accountType);
        if(!$memberAccount){
            $this->errors()->merge($this->order->member->errors());
            return false;
        }

        try{
            DB::beginTransaction();
            if(!$memberAccount->income($money, $accountType, $this->order->id, $description)){
                DB::rollback();
                return $this->errors()->merge($memberAccount->errors());
            }
        }catch (Exception $e){
            DB::rollback();
            return $this->errors()->add('error', $e->getMessage());
        }
        DB::commit();
        return true;
    }

    /**
     * 订单结算
     *
     * @param string $accountType
     * @return $this|bool
     */
    public function settle($accountType = MemberAccount::INTEGRAL_ACCOUNT){

        if($this->order->status !== Order::getStatusIDByKeyword(Order::GOT_ORDER)){
            return $this->errors()->add('error', '未付款订单才可进行结算!');
        }
        $memberAccount = $this->order->member->getAccount($accountType);
        if(!$memberAccount){
            $this->errors()->merge($this->order->member->errors());
            return false;
        }
        //获取系统设置的积分比率
        $accountTypeId = MemberAccount::getAccountTypeIDByKeyword($accountType);
        $integralScale = Setting::find(1)->integral_scale;
        $money = $this->order->total_price;
        //如果是积分结算，则需以积分价格结算
        if($accountType === MemberAccount::INTEGRAL_ACCOUNT){
            $money = $this->order->getIntegralPrice();
        }
        try{
            DB::beginTransaction();

            //设置已支付状态
            $result = $this->order->setStatus(Order::SETTLED);
            if(!$result){
                DB::rollback();
                $this->errors()->merge($this->order->errors());
                return false;
            }
            //扣款
            if(!$memberAccount->cost($money, $accountType, $this->order->id, MemberAccount::$accountTypes[$accountTypeId]['display_name'].'支付订单')){
                DB::rollback();
                $this->errors()->merge($memberAccount->errors());
                return false;
            }

            //其他消费类型赠送一定比例积分入账
            if($accountType !== MemberAccount::INTEGRAL_ACCOUNT){
                //获取积分账户
                $memberAccount = $this->order->member->getAccount(MemberAccount::INTEGRAL_ACCOUNT);
                if(!$memberAccount){
                    $this->errors()->merge($this->order->member->errors());
                    return false;
                }
                $integral = bcmul($money, $integralScale, 2);
                if(!$memberAccount->income($integral, MemberAccount::INTEGRAL_ACCOUNT, $this->order->id, MemberAccount::$accountTypes[$accountTypeId]['display_name'].'支付订单赠送积分')){
                    DB::rollback();
                    return false;
                }
            }

        }catch (Exception $e){
            DB::rollback();
            return $this->errors()->add('error', $e->getMessage());
        }
        DB::commit();
        return true;
    }

}